<?php 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 
/**
 * Volunteer Model
 *
 * @package    GippslandSite
 * @subpackage Components
 */
class VolunteerModelVolunteer extends JModel
{
    /**
    * Gets list of all locations in db
    * @return array of locations(id, name)
    */
    function getLocations()
    {
	    $db =& JFactory::getDBO();
	    $query = 'SELECT id, location AS name FROM gps_location ORDER BY location ASC';
	    $db->setQuery($query);
 		$rows = $db->loadObjectList();
 		return $rows;
    }
	
	/**
    * Gets list of all timspans in db
    * @return array of timspans(id, name)
    */
    function getTimespans()
    {
		$db =& JFactory::getDBO();
	    $query = 'SELECT id, timespan AS name FROM gps_timespan ORDER BY timespan ASC';
	    $db->setQuery($query);
 		$rows = $db->loadObjectList();
 		return $rows;
    }
	
	/**
    * Gets list of all industries in db
    * @return array of industries(id, name)
    */
    function getIndustries()
    {
		$db =& JFactory::getDBO();
	    $query = 'SELECT id, industry AS name FROM gps_industry ORDER BY industry ASC';
	    $db->setQuery($query);
 		$rows = $db->loadObjectList();
 		return $rows;
    }

	/**
	* Items total
	* @var integer
	*/
	var $_total = null;
 
	/**
	* Pagination object
	* @var object
	*/
	var $_pagination = null;
	
	function getPagination()
	{
		// Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
	} 
	
	function getTotal()
	{
        // Load the content if it doesn't already exist
        if (empty($this->_total))
		{
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);    
        }
        return $this->_total;
	}
	
	function getData() 
	{
        // if data hasn't already been obtained, load it
        if (empty($this->_data))
		{
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); 
        }
		
        return $this->_data;
	}
	
	function _buildQuery()
	{
		return 'SELECT * FROM gps_audiofile';
	}
	
	function __construct()
	{
        parent::__construct();
 
        global $mainframe, $option;
 
        // Get pagination request variables
        $limit      = JRequest::getVar('limit', 0, '', 'int'); 
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
	}
	

	/**
    * Gets location id,timespan id, industry id and other keyword list
    * @return search results as a object
    */

	function searchFile($location_id,$timespan_id,$industry_id,$opt_keywords)
	{
		 		
		//creating sql string for location id,timespan id ,industry id
		$sqlstr="SELECT * from gps_audiofile where 1=1";

		
		if($location_id>0 or $timespan_id>0 or $industry_id>0)
		{	
			if($location_id>0)
			{
				$sqlstr .= ' && gps_audiofile.location_id='.$location_id;		
			}
			if($timespan_id>0)
			{
				$sqlstr .= ' && gps_audiofile.timespan_id='.$timespan_id;
			}	
			if($industry_id>0)
			{
				$sqlstr .= ' && gps_audiofile.industry_id='.$industry_id;	
			}
		}
		
		//echo $sqlstr;//test				
		
		$opt_keywords=strip_tags($opt_keywords);
		
	//	 $type        = preg_replace('/[^A-Z0-9_\.-]/i', '', $type); 
		
		$opt_keywords_arr = explode(',',$opt_keywords);
 		//$opt_keywords_arr=trim($opt_keywords_arr);
		$flag=0;
		
		//creating sql string for optional keywords
	    if(sizeof($opt_keywords_arr)>0)
	    {
			foreach ($opt_keywords_arr as $i)
			{
				if($i!="")
				{
					$i=trim($i);
				 	$file_name=$i->id;
					if($flag=0)
					{
						$sqlstr .="AND gps_audiofile.id IN (AND 1=1) ";
						$flag=1;
					}
					$sqlstr .=" AND (select file_id from gps_filekeywords where gps_audiofile.id=gps_filekeywords.file_id and gps_filekeywords.keyword='".$i."')";					
					//echo "inside foreach";//test						 	 
	    		}
	    	} 
	    }
		
		//execute sql query
		$db =& JFactory::getDBO();
		$db->setQuery($sqlstr);
		if($rows = $db->loadObjectList())
		{
			//print_r($rows);//test
			//return "1";//test
			return $rows;
		}
		$obj = array();
		//print_r($obj); //test
	
		//return "1";//test
		return $obj;
	}
		
	
	/**
    * Gets file details update audio file details in db
    * @return true upon success
    */
	// function updateFile($fileId,$name, $description, $locationId, $timespanId, $industryId, $keywordList, $fileUrl, $acknowledment,$file_size,$last_modified_by)	
	// {
		// return true;
		// $audiofile_obj = new stdClass;
  		
		// $audiofile_obj->id = $fileId;
 		// $audiofile_obj->name =$name ;
		// $audiofile_obj->url =$fileUrl ;
		// $audiofile_obj->location_id =$locationId ;
		// $audiofile_obj->timespan_id =$timespanId ;
		// $audiofile_obj->industry_id =$industryId ;
		// $audiofile_obj->description =$description ;
		// $audiofile_obj->acknowledgement =$acknowledment ;
		// $audiofile_obj->file_size = $file_size;
		// $audiofile_obj->last_modified_by = $last_modified_by;
		// $audiofile_obj->last_modified_on = date("y.m.d");//-current date
	function updateFile($audiofile_obj, $keywordList)
 	{
 		$db =& JFactory::getDBO();
		
  		if(!$db->updateObject( "gps_audiofile", $audiofile_obj, 'id',"true"))
  		{
	  		echo $db->stderr();
    		return false;	 
  		}
  		// echo JTEXT::_('Updation Success'); //- test
  		 
  		 //update gps_keyword
  		$keyword_arr=explode(",",$keywordList);
		//$keyword_arr=trim($keyword_arr);
				////first remove all corresponding keywords from the filekeywords table
  		
		$delete_str="DELETE FROM gps_filekeywords WHERE file_id=".$audiofile_obj->id;
		$db->setQuery($delete_str);
		if (!$result = $db->query())
		{
		    echo $db->stderr();
		    return false;
		}		
  		////
  		
		if (sizeof($keyword_arr)>0)
		{
			foreach($keyword_arr as $item)
			{	
				if($item!="")
				{		
					$item=trim($item);
					$insert_str="INSERT INTO gps_keyword (keyword) VALUES ('".$item."')";
					$db->setQuery($insert_str);
					$db->query(); //ignore error message for primary key duplication
					
					$query1="INSERT INTO gps_filekeywords (file_id,keyword) VALUES (".$audiofile_obj->id.",'".$item."')";
					//echo JTEXT::_($query1); //test 
					$db->setQuery($query1);
					$result = $db->query();//ignore error message for primary key duplication
				}
			}	
		}
  		  //echo JTEXT::_('Updation Success FOR FILEKEYWORDS'); //- test
  		 //update gps_keyword
  		 return true;		 
	}	
	

	/**
    * Gets file details add a new audio file to the db
    * @return true upon success
    */

	function addFile($name, $description, $locationId, $timespanId, $industryId, $keywordList, $fileUrl, $acknowledment,$file_size,$last_modified_by)	
	{
		
			
		/////////test
		//  $fileId=49;
		  // $name = "testing_joomlaaaa hello";
		  // $fileUrl="localhost/mp3ssss";
		  // $locationId=2;
		  // $timespanId=3;
		  // $industryId=1;
		  // $description="This is a insert gzzzzzzzz!!!!!!!!!!!!!!!";
		  // $acknowledment="inserting success";
		  // $file_size="15Mb";
		  // $last_modified_by="gh";
// //		  $last_modified_on="date";
		  // $keywordList="French island,vision 2";
		////////test end
		
		$audiofile_obj = new stdClass;
  		
		$audiofile_obj->id = $fileId;
 		$audiofile_obj->name =$name ;
		$audiofile_obj->url =$fileUrl ;
		$audiofile_obj->location_id =$locationId ;
		$audiofile_obj->timespan_id =$timespanId ;
		$audiofile_obj->industry_id =$industryId ;
		$audiofile_obj->description =$description ;
		$audiofile_obj->acknowledgement =$acknowledment ;
		$audiofile_obj->file_size = $file_size;
		$audiofile_obj->last_modified_by = $last_modified_by;	
		$audiofile_obj->last_modified_on = date("y.m.d"); // - Add current date
 	
 		$db =& JFactory::getDBO();
  
  		 if(!$db->insertObject( "gps_audiofile", $audiofile_obj, 'id',"true"))
  		 {
	  		//echo $db->stderr();
    		return false;	 
  		 }
  		 
  	//	 echo JTEXT::_("insertion success file id=".$audiofile_obj->id); //test
  		 $new_fileId=$audiofile_obj->id;
  
  		$keyword_arr=explode(",",$keywordList);
		//$keyword_arr=trim($keyword_arr);
		
		if (sizeof($keyword_arr)>0)
		{
			foreach($keyword_arr as $item)
			{
				if($item!="")
				{
					$item=trim($item);
					$insert_str="INSERT INTO gps_keyword (keyword) VALUES ('".$item."')";
					$db->setQuery($insert_str);
					$db->query(); //ignore error message for primary key duplication
					
					$query1="INSERT INTO gps_filekeywords (file_id,keyword) VALUES (".$new_fileId.",'".$item."')";
					//echo JTEXT::_($query1);
					$db->setQuery($query1);
					if (!$result = $db->query())
					{
					//    echo $db->stderr(); //test
					    return false;
					}
				}
			}	
		}
  		 	//		echo JTEXT::_('insertion success -from gps_filekeywords'); //test
  		 return true;
	}
	
	
	/**
    * Gets file id and delete file
    * @return true upon success 
    */

	function deleteFile($fileId)
	{
		
		//$fileId=65;
		$db =& JFactory::getDBO();
		$query = 'DELETE FROM gps_audiofile WHERE id='.$fileId;
		$db->setQuery($query);
		if (!$result = $db->query()) {
		    // echo $db->stderr();
		 //   return "0";
		    return false;
		}
		//echo JTEXT::_('Deletion success from gps_adudiotable'); //test
		return true;
	
// 		$fileId=50;
// 	//	$db =& JFactory::getDBO();
// 		$query2 = 'DELETE FROM gps_filekeywords WHERE file_id='.$fileId;
// 		$db->setQuery($query2);
// 		if (!$result = $db->query()) {
// 		    echo $db->stderr();
// 		    return "0";
// 		    //return false;
// 		}
// 		echo JTEXT::_('Deletion success from gps_filekeywords'); //test
// 		return "1";
		//return true;
	}


	/**
    * Gets file details
    * @return audio file object 
    */

	function getFile($fileId)
	{
		//$fileId=49;
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM gps_audiofile WHERE id=".$fileId;
		//echo $query;
	    $db->setQuery($query);
	    $audiofile=$db->loadObject();
 		//echo JTEXT::_('Deletion success from gps_filekeywords' . $db->loadObject()->name);		
	    return $audiofile;
	}	


	/**
    * Gets details of user 
    * @return object containing username and isadmin
	* if authentication fails - return $userobj->user_id=""
    */

	function authenticate($user_id, $password) 
	{
		//test
		//$user_id="ghda";
		//$password="aaa";
		//test
		if($user_id=="" or $password=="" )
		{
			$userobj->user_id="";
			return $userobj;
		}
		
		$db =& JFactory::getDBO();
		$query = "SELECT user_id,is_admin FROM gps_user WHERE user_id='".$user_id."' and password='".md5($password)."'";
		// echo $query; //test
	   
		$db->setQuery($query);
	    if($userobj = $db->loadObject())
		{
		//	 print_r($userobj);
		}
		else
		{
			$userobj->user_id="";
			return $userobj;
		}
	  return $userobj;	  
	}	
		
		
	//********Add, delete, search, view keywords - ************START************	
	
	/*
    * Gets keyword and delete keyword
    * @return true upon success 
    */
	function deleteKeyword($keyword)
	{
		$keyword=trim($keyword);
		//$keyword="temp";//test
		if(!$keyword=="")
		{
			$db =& JFactory::getDBO();
			$query = "DELETE FROM gps_keyword WHERE keyword='".$keyword."'";
			//echo $query; //test
		   
			$db->setQuery($query);
		  
			if (!$result = $db->query()) {
				//echo $db->stderr();	//test
				return false;
			}
			//echo JTEXT::_('Deletion success from gps_keyword'); //test
			return true;
		}
		return false;
	}	
	
	
	/*
    * @return all keywords in the db 
    */
	function getKeywords()
	{
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM gps_keyword";
		//echo $query; //test
	   	$db->setQuery($query);
 		$rows = $db->loadObjectList();
		//print_r($rows); //test
 		return $rows;
	}
	
	
	/*
    * Gets new keyword and add keyword into the db
    * @return true upon success 
    */
	function addKeyword($keyword)
	{
		$keyword=trim($keyword);
		//$keyword="tempx";//test
		if($keyword!="")
		{
			$keyword_obj = new stdClass; 
			$keyword_obj->keyword=$keyword;
			$db =& JFactory::getDBO();
			if(!$db->insertObject( "gps_keyword", $keyword_obj, 'keyword',"true"))
			{
				//echo $db->stderr();//test
				return false;	 
			}
		}
		//echo $keyword_obj->id;//test
		return true;
	}
	
	
	/*
    * Gets a string and search keywords according to the string
    * @return all keywords according to the string
    */
	function searchKeyword($str)
	{
		//$str="j";//test
		$str=trim($str);
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM gps_keyword WHERE keyword like '%".$str."%'";
		//echo $query; //test
	   	$db->setQuery($query);
 		$rows = $db->loadObjectList();
		//print_r($rows); //test
 		return $rows;
	}	
	
	/*
    * Gets a keyword
    * @return true if keyword exists in db
    */	
	function existKeyword($keyword)	
	{
		//$keyword="keyword_x";
		$str=trim($keyword);
		$db =& JFactory::getDBO();
		$query = "SELECT * FROM gps_keyword WHERE keyword='".$str."'";
	   	$db->setQuery($query);
 		if(!$row = $db->loadRow())
		{
			return false;
		}
		//print_r($row); //test
 		return true;
	}
	
	/*
    * Gets old keyword and new keyword and update keywords table
    * @return true if updation is successful
    */	
	function updateKeyword($oldKeyword, $newKeyword)
	{
		//$str="j";//test
		//$oldKeyword="joomla";
		//$newKeyword="joomlax";
		
		$db =& JFactory::getDBO();
		$query ="UPDATE gps_keyword SET keyword='".$newKeyword."' WHERE keyword='".$oldKeyword."'";
		
	   	$db->setQuery($query);
 		if (!$result = $db->query())
		{
			//echo $db->stderr();	//test		
			return false;
		}
			//echo JTEXT::_('Deletion success from gps_keyword'); //test
		return true;
	}
	
	
	//********Add, delete, search, view keywords - ************END************	
	
//**********Add, exists functions for location,timespan,industry - start******************
	//
	
	/*
    * Gets a location name
    * @return true if location exists in db
    */	
	function existLocation($name)
	{
		//$name="burwood";//test
		//$name=trim($name);
		if($name!="")
		{
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM gps_location WHERE location='".$name."'";
			//echo $query; //test
			$db->setQuery($query);
			if($rows = $db->loadRow())
			{
				return true;
			}
			return false;
		}
		else
		{
		//print_r($rows); //test
			return false;
		}
	}
	
	
	
	/*
    * Gets a timespan
    * @return true if timespan exists in db
    */	
	function existTimespan($name)
	{
		//$name="3010-3020";//test
		$name=trim($name);
		if($name!="")
		{
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM gps_timespan WHERE timespan='".$name."'";
			//echo $query; //test
			$db->setQuery($query);
			if($rows = $db->loadRow())
			{
				return true;
			}
			return false;
		}
		else
		{
		//print_r($rows); //test
			return false;
		}
	}
	
	
	/*
    * Gets a industry
    * @return true if industy exists in db
    */	
	function existIndustry($name)
	{
		//$name="coal";//test
		$name=trim($name);
		if($name!="")
		{
			$db =& JFactory::getDBO();
			$query = "SELECT * FROM gps_industry WHERE industry='".$name."'";
			//echo $query; //test
			$db->setQuery($query);
			if($rows = $db->loadRow())
			{
				return true;
			}
			return false;
		}
		else
		{
		//print_r($rows); //test
			return false;
		}
	}
	
	
	/*
    * Gets new location and add into the db
    * @return true upon success 
    */
	function addLocation($name)
	{
		//$name="tempx";//test
		$name=trim($name);
		if($name!="")
		{
			$loc_obj = new stdClass; 
			$loc_obj->location=$name;
			$db =& JFactory::getDBO();
			if(!$db->insertObject( "gps_location", $loc_obj, 'id',"true"))
			{
				//echo $db->stderr();//test
				return false;	 
			}
			return true;
		}
		else
		{
		//echo $keyword_obj->id;//test
			return false;
		}
	}
	
	/*
    * Gets new timespan and add into the db
    * @return true upon success 
    */
	function addTimespan($name)
	{
		//$name="3010-3020";//test
		$name=trim($name);
		if($name!="")
		{
			$timespan_obj = new stdClass; 
			$timespan_obj->timespan=$name;
			$db =& JFactory::getDBO();
			if(!$db->insertObject( "gps_timespan", $timespan_obj, 'id',"true"))
			{
				//echo $db->stderr();//test
				return false;	 
			}
			return true;
		}
		else
		{
		//echo $keyword_obj->id;//test
			return false;
		}
	}
	
	/*
    * Gets new industry and add into the db
    * @return true upon success 
    */
	function addIndustry($name)
	{
		//$name="temp_industry";//test
		$name=trim($name);
		if($name!="")
		{
			$industry_obj = new stdClass; 
			$industry_obj->industry=$name;
			$db =& JFactory::getDBO();
			if(!$db->insertObject( "gps_industry", $industry_obj, 'id',"true"))
			{
				//echo $db->stderr();//test
				return false;	 
			}
			return true;
		}
		else
		{
		//echo $keyword_obj->id;//test
			return false;
		}
	}

	/////
	/*
    * Gets query and execute
    * @return true upon success 
    */
	private function executeQuery($query)
	{
		$db =& JFactory::getDBO();
	    $db->setQuery($query);

		if (!$result = $db->query()) 
		{	
			// echo $db->stderr();	//test
			$res=false;			
		}
		else
		{
			$res=true;
		}
		return $res;
	}
	/*
    * Gets location and delete it
    * @return true upon success 
    */
	function deleteLocation($id)          
	{
		//$name="tempx";//test
		// $name=trim($name);
		if(!$id=="")
		{
			$query = "DELETE FROM gps_location WHERE id=" . $id;
			echo $query;
			$result=$this->executeQuery($query);
			return $result;
		}
		return false;
	}	
	
	/*
    * Gets timespan and delete it
    * @return true upon success 
    */
	function deleteTimespan($name)          
	{
		//$name="3010-3020";//test
		$name=trim($name);
		if(!$name=="")
		{
			$query = "DELETE FROM gps_timespan WHERE id='".$name."'";
			$result=$this->executeQuery($query);
			//echo "result = ".$result;
			return $result;
		}
		return false;
	}	
	
		/*
    * Gets industry and delete it
    * @return true upon success 
    */
	function deleteIndustry($name)	{
		//$name="temp_industry";//test
		$name=trim($name);
		if(!$name=="")
		{
			$query = "DELETE FROM gps_industry WHERE id='".$name."'";
			$result=$this->executeQuery($query);
			return $result;
		}
		return false;
	}
	
	/* 
    * Gets a sql query and execute
    * @return rows as object array 
    */
	private function loadSearchresults($query)
	{
		$db =& JFactory::getDBO();
	    $db->setQuery($query);
		$objList = $db->loadObjectList();
		
		if(sizeof($objList) == 0)
			return array();
		else
			return $objList;
	}
	/* 
    * Gets location name and search db
    * @return rows as object array 
    */
	function searchLocation($name)
	{
		//$name="wo";
		return $this->loadSearchresults("SELECT * FROM gps_location WHERE location like '%".$name."%' ORDER BY location ASC");
	}
	
	/* 
    * Gets timespan and search db
    * @return rows as object array 
    */
	function searchTimespan($name)
	{
		//$name="90";
		return $this->loadSearchresults("SELECT * FROM gps_timespan WHERE timespan like '%".$name."%' ORDER BY timespan ASC");
	}
	
	/* 
    * Gets industry and search db
    * @return rows as object array 
    */
	function searchIndustry($name) 
	{
		//$name="co";
		return $this->loadSearchresults("SELECT * FROM gps_industry WHERE industry like '%".$name."%' ORDER BY industry ASC");
	}
	
	/* *************override loadObject function***********
    * Gets sql query
    * @return rows as object array 
    */
	private function loadSelectquery($query)
	{
		$db =& JFactory::getDBO();
	    $db->setQuery($query);
		return $db->loadObject();
	}
	
	function getFileKeywords($fileId)
	{
		// echo 'SELECT keyword FROM gps_filekeywords WHERE file_id = ' . $fileId;
		return $this->loadSearchresults('SELECT keyword FROM gps_filekeywords WHERE file_id = ' . $fileId);
	}
	
	/* 
    * Gets Location id
    * @return id and Location name as object 
    */
	function getLocation($locId)
	{
		return $this->loadSelectquery('SELECT id,location FROM gps_location WHERE id = ' . $locId);
	}
	
	/* 
    * Gets Timespan id
    * @return id and Timespan name as object 
    */
	function getTimespan($timId)
	{
		return $this->loadSelectquery('SELECT id,timespan FROM gps_timespan WHERE id = ' . $timId);
	}
	
	/* 
    * Gets industry 
    * @return id and industry name as object 
    */
	function getIndustry($indId)
	{
		return $this->loadSelectquery('SELECT id,industry FROM gps_industry WHERE id = ' . $indId);
	}
	
	/* 
    * Gets location id and name
    * @return success upon update
    */
	function updateLocation($locationId, $name)
	{
		//$locationId=9;
		//$name="temp_modify";
		
		$name=trim($name);
		if(!$name=="")
		{
			$query = "UPDATE gps_location SET location='".$name."' WHERE id='".$locationId."'";
			$result=$this->executeQuery($query);
			//echo "result = ".$result;//test
			return $result;
		}
		return false;
	}
	
	/* 
    * Gets timespan id and timespan
    * @return success upon update
    */
	function updateTimespan($id, $name)
	{
		// $id=7;//test
		// $name="3000-3050";//test
		$name=trim($name);
		if(!$name=="")
		{
			$query = "UPDATE gps_timespan SET timespan='".$name."' WHERE id='".$id."'";
			$result=$this->executeQuery($query);
			//echo "result = ".$result;//test
			return $result;
		}
		return false;
	}
	/* 
    * Gets industry id and name
    * @return success upon update
    */
	function updateIndustry($id, $name)
	{
		//$id=5;//test
		//$name="industry_modify";//test
		
		$name=trim($name);
		if(!$name=="")
		{
			$query = "UPDATE gps_industry SET industry='".$name."' WHERE id='".$id."'";
			$result=$this->executeQuery($query);
			//echo "result = ".$result;//test
			return $result;
		}
		return false;
	}
	
//**********Add, exists functions for location,timespan,industry - END******************	
	
	/* 
    * @return id, name and url as a object 
    */
	function getLinks()
	{
		return $this->loadSearchresults('SELECT id,name,url FROM gps_links');
	}
	
	/* 
    * Gets sql url and name then insert them into db
    * @return true upon success
    */
	function addLink($url,$name)
	{		
		// $url="This is the url";
		// $name="This is the name";
		$url=trim($url);
		if($url!="")
		{
			return $this->executequery("INSERT INTO gps_links (name,url) VALUES ('".$name."','".$url."')");
		}
		return false;
	}
	
	
	/* 
    * Gets sql query string and check for existance in db
    * @return true if found
    */
	private function existEntries($query)
	{
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		if($rows = $db->loadRow())
		{
			return true;
		}
		return false;
	}
	
	function existLink($url)
	{
		//$url="http://www.example.com";
		return $this->existEntries("SELECT * FROM gps_links WHERE url='".$url."'");
	}
	
	
	/* 
    * Gets name and search the db
    * @return rows as object array 
    */
	function searchLink($name) 
	{
		//$name="example";
		return $this->loadSearchresults("SELECT * FROM gps_links WHERE name like '%".$name."%'");
	}
	
	/* Gets link id * @return rows as object array */
	function getLink($id)
	{
		//$id=1;
		return $this->loadSelectquery('SELECT id,name,url FROM gps_links WHERE id = ' . $id);
	}
	
	/* 
    * Gets id, name, url 
    * @return success upon update
    */
	function updateLink($id,$url,$name)
	{
	// return true;
		// $id=2;//test
		// $name="XXXXXs";//test
		// $url="ZZZZZZs";//test
		$name=trim($name);
		$url=trim($url);
		if(!$name=="" && !$url=="")
		{
			return $this->executeQuery("UPDATE gps_links SET name='".$name."',url='".$url."' WHERE id=".$id);
		}
		return false;
	}
	
	/*
    * Gets link id and deletes it
    * @return true upon success 
    */
	function deleteLink($id) 
	{	
		//$id=2;//tset
		$name=trim($id);
		if(!$id=="")
		{
			return $this->executeQuery("DELETE FROM gps_links WHERE id=".$id);
		}
		return false;
	}	
	
	
	//links - end
	
	//volunteer - start
	
	/*  @return list of volunteers  */
	function getVolunteers()
	{
		$query = "SELECT gps_user.user_id as username, COUNT(gps_audiofile.id) AS totalFileUploaded FROM gps_audiofile ";
		$query .= "RIGHT JOIN gps_user ON gps_audiofile.last_modified_by = gps_user.user_id WHERE gps_user.is_admin = 'n' GROUP BY gps_user.user_id";
		// echo $query;
		return $this->loadSearchresults($query);
		//'SELECT last_modified_by,count(id) FROM gps_audiofile GROUP BY last_modified_by'
	}
	
	
	/* Adds a volunteer to the db    */
	function addVolunteer($username, $password)
	{
		$username=trim($username);
		$password=trim($password);
		if($username!="" && $password!="")
		{
			return $this->executequery("INSERT INTO gps_user VALUES ('".$username."','".md5($password)."', 'n')");
		}
		return false;
	}
	
	/* checks for username exists in the db    */
	function existVolunteer($username)
	{
		return $this->existEntries("SELECT * FROM gps_user WHERE user_id='".$username."'");
	}
	
	
	/* update volunteer info   */
	function updateVolunteer($oldUsername, $newUsername, $newPass)
	{
		// $oldUsername=volunteerX;//test
		// $newUsername="vol1";//test
		// $newPass="Zssas";//test
		$oldUsername=trim($oldUsername);
		$newUsername=trim($newUsername);
		$newPass=trim($newPass);
		//echo "sss";
		if(!$oldUsername=="" && !$newUsername=="" && !$newPass=="")
		{
			return $this->executeQuery("UPDATE gps_user SET user_id='".$newUsername."',password='".md5($newPass)."' WHERE user_id='".$oldUsername."'");
		}
		return false;
	}
	
	/* deletes a volunteer from the db */
	function deleteVolunteer($username)   
	{
		$username=trim($username);
		if(!$username=="")
		{
			return $this->executeQuery("DELETE FROM gps_user WHERE user_id='".$username."'");
		}
		return false;
	}
	
	
	// /* returns list of matched volunteers  */
	function searchVolunteer($username)
	{
		$query = "SELECT gps_user.user_id as username, COUNT(gps_audiofile.id) AS totalFileUploaded FROM gps_audiofile ";
		$query .= "RIGHT JOIN gps_user ON gps_audiofile.last_modified_by = gps_user.user_id WHERE user_id like '%".$username."%' AND gps_user.is_admin = 'n'  GROUP BY gps_user.user_id";
		// echo $query;
		return $this->loadSearchresults($query);
	}
	
	
	//volunteer - end
	
	//volunteer files - start
	
	/* returns list of files(*) uploaded by specified user*/
	function getFilesByUser($username)
	{
		$username=trim($username);
		if(!$username=="")
		{
			return $this->loadSearchresults("SELECT * FROM gps_audiofile WHERE last_modified_by='".$username."'");
		}
		return false;
	}
	
	/*check if entered password is correct*/
	function matchPassword($username, $oldpassword)
	{
		$username=trim($username);
		$oldpassword=trim($oldpassword);
		if(!$username=="" && !$oldpassword=="")
		{
			return $this->existEntries("SELECT user_id FROM gps_user WHERE user_id='".$username."' AND password='".md5($oldpassword)."'");
		}
		$obj = array();
		return $obj;
	}
	
	/*update user password*/
	function updatePassword($username, $newpassword)
	{
		$username=trim($username);
		$newpassword=trim($newpassword);
		if(!$username=="" && !$newpassword=="")
		{
			return $this->executeQuery("UPDATE gps_user SET password='".md5($newpassword)."' WHERE user_id='".$username."'");
		}
		return false;
	}
	
	//volunteer files - end
	
	
	
	
	//feedback//

	/*
    * 
    * @return feedbacks descending order of date
    */		
	function getFeedbacks()
	{
		$query = "SELECT gps_audiofile.name AS 'filename', gps_filecomments.file_id, gps_filecomments.`comment`, gps_filecomments.name, ";
		$query .="gps_filecomments.`date`, gps_audiofile.last_modified_by, gps_filecomments.id FROM gps_filecomments INNER JOIN gps_audiofile ";
		$query .="ON gps_filecomments.file_id = gps_audiofile.id ORDER BY gps_filecomments.`date` DESC";
		
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		if($rows = $db->loadObjectList())
		{
			//var_dump($rows);
			return $rows;
		}
		$obj = array();
		return $obj;
	}
	
	/* adds feedbacks into db*/
	function addFeedback($fileId, $name, $feedback)
	{
		//$fileId="1";
		//$name="zcccv";
		//$feedback="description bbbbbbbbbbbbbbb";
		$today = date("y.m.d");
		$fileId=trim($fileId);
		if($fileId!="")
		{
			return $this->executequery("INSERT INTO gps_filecomments (file_id,name,comment,date) VALUES (".$fileId.",'".$name."','".$feedback."','".$today."')");
		}
		return false;
	
	}
	
	/*delete feedbacks from db*/
	function deleteFeedback($id)
	{
		// $id="3";//test
		$id=trim($id);
		if(!$id=="")
		{
			echo "DELETE FROM gps_filecomments WHERE id=".$id;
			return $this->executeQuery("DELETE FROM gps_filecomments WHERE id=".$id);
		}
		return false;
	}
	// {
		// $id="3";//test
		// $id=trim($id);
		// if(!$id=="")
		// {
			// $result=$this->executeQuery("DELETE FROM gps_filecomments WHERE id=".$id);
			// echo "result = ".$result;
			// return $result;
		// }
		// return "false";
	// }
	
	
	//feedback - end
}